#include "stdafx.h"
#include "NodePoint.h"
#include "Utility.h"
#include <math.h>


RECT NodePoint::DrawArea;
int NodePoint::xInterval;
int NodePoint::yInterval;
int NodePoint::Radius;



void NodePoint::DefaultParamSetting()
{
	mFontSize=15;
	mNodeNum=-1;
}

NodePoint::NodePoint( Point2d center, int radius/*=7*/, Color color/*=Color(255,255,0,0)*/ )
{
	mCenterPoint=center;
	Radius=radius;
	mNodeColor=color;
	DefaultParamSetting();
}

NodePoint::NodePoint()
{
	mNodeColor=Color(0,0,255);
	DefaultParamSetting();
}

NodePoint::NodePoint( int x, int y, int radius/*=7*/, Color color/*=Color(255, 255, 0, 0)*/ )
{
	Point2d center={x, y};
	mCenterPoint=center;
	Radius=radius;
	mNodeColor=color;
	DefaultParamSetting();
}

void NodePoint::setNodePointRadius( int radius )
{
	Radius=radius;
}

void NodePoint::DrawNodePoint( Graphics* graphics )
{
	if (!this->IsInDrawArea())
	{
		TRACE("point is out of range");
		return;
	}
	SolidBrush solidBrush(mNodeColor);
	Rect rect(mCenterPoint.x-Radius, mCenterPoint.y-Radius, 2*Radius+1, 2*Radius+1);
	graphics->FillEllipse(&solidBrush,rect);
	DrawNodeNumber(graphics);
}

void NodePoint::setDrawArea( RECT area )
{
	NodePoint::DrawArea=area;
}

Point2d NodePoint::getNodePointCenter()
{
	return mCenterPoint;
}

bool NodePoint::IsInDrawArea()
{
	int pleft=mCenterPoint.x-Radius;
	int ptop=mCenterPoint.y-Radius;
	int pright=mCenterPoint.x+Radius;
	int pbottom=mCenterPoint.y+Radius;
	
	if ((pleft>DrawArea.left)&&(ptop>DrawArea.top)
		&&(pright<DrawArea.right)&&(pbottom<DrawArea.bottom))
	{
		return true;
	}
	else
		return false;
}

void NodePoint::setNodePointColor( Color color )
{
	mNodeColor=color;
}

void NodePoint::setNodePointCenter( int x, int y )
{
	Point2d pos={x, y};
	this->setNodePointCenter(pos);
}

void NodePoint::setNodePointCenter( Point2d center )
{
	this->mCenterPoint=center;
}



Point2d NodePoint::CalculateOutgoingLineStartPosition( Point2d disPoint )
{
	float delta_x=disPoint.x-mCenterPoint.x;
	float delta_y=disPoint.y-mCenterPoint.y;
	if (abs(delta_y)<0.00001)
	{
		delta_y=0.00001;
	}
	if (abs(delta_x)<0.00001)
	{
		delta_x=0.00001;
	}
	float angle=atan(((float)abs(delta_y))/abs(delta_x));

	int move_x=ceil((Radius+1)*cos(angle)*(delta_x/abs(delta_x)));
	int move_y=ceil((Radius+1)*sin(angle)*(delta_y/abs(delta_y)));
	Point2d outgointPoint={mCenterPoint.x+move_x, mCenterPoint.y+move_y};
	return outgointPoint;
}


void NodePoint::DrawNodeNumber( Graphics* graphics )
{
	FontFamily  fontFamily(L"Times New Roman");
	Font        font(&fontFamily, mFontSize, FontStyleBold, UnitPixel);
	//print node number at the right and half top
	PointF      pointF(mCenterPoint.x+Radius+1, mCenterPoint.y-2*Radius-2);
	SolidBrush  solidBrush(Color(255, 0, 0, 0));
	string nodeNumber=Utility::ConvertNumberToString(mNodeNum);
	//WTF? sick...
	WCHAR wc[100]; 
	swprintf(wc,L"%S",nodeNumber.c_str());    
	graphics->DrawString(wc, -1, &font, pointF, &solidBrush);
}

void NodePoint::setNodeNum( NODENUM number )
{
	mNodeNum=number;
}

NODENUM NodePoint::getNodeNum()
{
	return mNodeNum;
}

NeighbourInfoArray& NodePoint::getNeighbourInfoArray()
{
	return mNeighbourInfoArray;
}

void NodePoint::setNodeCoarsePos( int x, int y )
{
	xCoarsePos=x;
	yCoarsePos=y;
}

void NodePoint::setDrawInterval( int maxX, int maxY )
{
	if (maxX>1)
		maxX-=1;
	if (maxY>1)
		maxY-=1;
	xInterval=(DrawArea.right-DrawArea.left-2*Radius-10)/maxX;
	yInterval=(DrawArea.bottom-DrawArea.top-2*Radius-10)/maxY;
}

void NodePoint::setNodeRealCenterPos()
{
	int sx=0, sy=0;
	sx=DrawArea.left+Radius+1;
	sy=DrawArea.top+Radius+1;
	setNodePointCenter(sx+xInterval*(xCoarsePos-1), sy+yInterval*(yCoarsePos-1));
}

